package com.salton123.soulove.biz_bmob.impl

import android.annotation.SuppressLint
import android.content.Context
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import cn.bmob.v3.listener.UploadBatchListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.salton123.soulove.api.IUserProvider
import com.salton123.soulove.biz_bmob.helper.BmobHelper
import com.salton123.soulove.biz_bmob.model.User
import com.salton123.soulove.common.Constants

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/4/23 11:54 PM
 * ModifyTime: 11:54 PM
 * Description:
 */
@SuppressLint("CheckResult")
@Route(path = Constants.Provider.USER)
class UserProvider : IUserProvider {

    override fun login(account: String, password: String, callback: (ret: Boolean, err: Throwable?) -> Unit) {
        BmobHelper.login(account, password).subscribe({
            callback.invoke(true, null)
        }, {
            callback.invoke(false, it)
        })
    }

    override fun register(account: String, password: String, callback: (ret: Boolean, err: Throwable?) -> Unit) {
        BmobHelper.register(account, password).subscribe({
            callback.invoke(true, null)
        }, {
            callback.invoke(false, it)
        })
    }

    override fun getLoginUser(): User? = BmobHelper.getLoginUser()

    override fun isLogin(): Boolean = BmobUser.isLogin()
    override fun autoLogin() {
        BmobHelper.autoLogin()
    }

    override fun getLoginUserId(): String = BmobHelper.loginUserId
    override fun logout() {
        BmobHelper.logout()
    }

    override fun active() {
        BmobHelper.active()
    }

    override fun uploadAvatar(filePath: String, callback: (ret: Boolean, err: Throwable?) -> Unit) {
        BmobHelper.uploadFile(filePath) { progress, ret, err ->
            if (progress == -1) {
                callback.invoke(false, null)
            } else if (progress == 100 && ret != null) {
                BmobHelper.getLoginUser()?.apply {
                    avatar = ret
                    update(object : UpdateListener() {
                        override fun done(p0: BmobException?) {
                            if (p0 == null) {
                                callback.invoke(true, null)
                            } else {
                                callback.invoke(false, p0)
                            }
                        }
                    })
                }
            }

        }
    }

    override fun updateUserInfo(infoKv: Map<String, String>, callback: (ret: Boolean) -> Unit) {
        BmobHelper.getLoginUser()?.apply {
            infoKv["nickname"]?.let { nickname = it }
            infoKv["avatar"]?.let { avatar?.url = it }
            infoKv["email"]?.let { email = it }
            infoKv["mobilePhoneNumber"]?.let { mobilePhoneNumber = it }
            infoKv["age"]?.let { age = it.toIntOrNull() ?: 0 }
            infoKv["gender"]?.let { gender = it.toIntOrNull() ?: 0 }
            infoKv["province"]?.let { province = it }
            infoKv["city"]?.let { city = it }
            infoKv["country"]?.let { country = it }
            update(object : UpdateListener() {
                override fun done(p0: BmobException?) {
                    if (p0 == null) {
                        callback.invoke(true)
                    } else {
                        callback.invoke(false)
                    }
                }
            })
        }
    }

//    @Suppress("UNCHECKED_CAST")
//    override fun <T> getUserInfo(infoKey: String, type: Class<T>): T {
//        BmobHelper.getLoginUser()?.apply {
//            when (infoKey) {
//                "nickname" -> {
//                    return nickname as T
//                }
//                "avatar" -> {
//                    return avatar as T
//                }
//                "email" -> {
//                    return email as T
//                }
//                "mobilePhoneNumber" -> {
//                    return mobilePhoneNumber as T
//                }
//                "age" -> {
//                    return age as T
//                }
//                "gender" -> {
//                    return gender as T
//                }
//                "province" -> {
//                    return province as T
//                }
//                "city" -> {
//                    return city as T
//                }
//                "country" -> {
//                    return country as T
//                }
//                else -> {
//                    throw RuntimeException("class cast exception")
//                }
//            }
//        }
//    }

    override fun upload(fileList: MutableList<String>, listener: UploadBatchListener) {
        BmobHelper.uploadBatch(fileList, listener).subscribe()
    }

    override fun init(context: Context?) {
    }
}